package com.example.weli.ble2test;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

public class Tcp_connect {
    public static final int TCP_STATE_CONNECTED = 3;
    public static final int TCP_STATE_CONNECTING = 2;
    public static final int TCP_STATE_LISTEN = 1;
    public static final int TCP_STATE_NONE = 0;
    private int State;
    private ConnectingThread connecting;
    private final Handler mHandler;
    private Socket socket;
    private SocketAddress socketAddress;
    private TCPListenerThread tcp_rc;

    private class ConnectingThread extends Thread {
        private Socket mSocket;

        private ConnectingThread() {
        }

        public void Set_Socket(Socket in) {
            this.mSocket = in;
        }

        public void run() {
            while (true) {
                try {
                    sleep(500);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                if (Tcp_connect.this.State == Tcp_connect.TCP_STATE_CONNECTING) {
                    if (Tcp_connect.this.socket.isClosed()) {
                    }
                    if (Tcp_connect.this.socket.isConnected()) {
                        try {
                            Tcp_connect.this.State = Tcp_connect.TCP_STATE_CONNECTED;
                            Tcp_connect.this.tcp_rc = new TCPListenerThread(Tcp_connect.this.socket.getInputStream());
                            Tcp_connect.this.tcp_rc.start();
                            Tcp_connect.this.mHandler.obtainMessage(7, Tcp_connect.this.State, -1).sendToTarget();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    public class TCPListenerThread extends Thread {
        private InputStream inputStream;

        public TCPListenerThread(InputStream inputStream) {
            this.inputStream = inputStream;
        }

        public void run() {
            byte[] buffer = new byte[1024];
            while (true) {
                try {
                    int readSize = this.inputStream.read(buffer);
                    if (readSize <= 0) {
                        this.inputStream.close();
                        Tcp_connect.this.State = 0;
                        Tcp_connect.this.mHandler.obtainMessage(7, Tcp_connect.this.State, -1).sendToTarget();
                        Log.d("error:", "close connect...");
                        break;
                    }
                    Tcp_connect.this.mHandler.obtainMessage(6, readSize, -1, buffer).sendToTarget();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public Tcp_connect(Context context, Handler handler) {
        this.State = 0;
        this.State = 0;
        this.mHandler = handler;
    }

    public int get_stauts() {
        return this.State;
    }

    public int MTCP_Connect(String add, int port) {
        try {
            return TCP_Connect(add, port);
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public void TCP_DisConnect() {
        if (this.State == TCP_STATE_CONNECTED) {
            try {
                this.socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public int TCP_Connect(String add, int port) throws IOException {
        if (this.State == TCP_STATE_CONNECTED) {
            this.socket.close();
            this.State = 0;
        }
        this.socketAddress = new InetSocketAddress(add, port);
        this.socket = new Socket();
        try {
            Log.d("Try:", add + "@" + String.valueOf(port));
            this.socket.connect(this.socketAddress, 1000);
            Log.d("error:", "intgsfaewfew");
            this.State = TCP_STATE_CONNECTING;
            if (this.connecting == null) {
                this.connecting = new ConnectingThread();
                this.connecting.start();
            }
            Log.d("TCP", "connecting...");
            return TCP_STATE_LISTEN;
        } catch (IOException e) {
            e.printStackTrace();
            this.State = 0;
            Message msg = this.mHandler.obtainMessage(5);
            Bundle bundle = new Bundle();
            bundle.putString(UIActivity.TOAST, "TCP\u8fde\u63a5\u5931\u8d25");
            msg.setData(bundle);
            this.mHandler.sendMessage(msg);
            Log.d("error:", "connect error...");
            return 0;
        }
    }

    public void TCP_Send_Bytes(byte[] data) {
        if (this.State == TCP_STATE_CONNECTED) {
            try {
                OutputStream outputStream = this.socket.getOutputStream();
                outputStream.write(data);
                outputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
                Log.d("error:", "Send data error...");
            }
        }
    }
}
